<template>
  <div id="app-container" style="padding:30px;">
    <el-form label-width="130px">
      <el-form-item label="书名">
        <el-col :span="5">
          <el-input v-model="book.name"/>
        </el-col>
      </el-form-item>
      <el-form-item label="作者">
        <el-col :span="5">
          <el-input v-model="book.author"/>
        </el-col>
      </el-form-item>
      <!-- TODO 版权 -->
      <!-- TODO 批次 -->
      <!-- TODO 书单 -->
      <el-form-item label="授权开始时间">
        <el-date-picker
          v-model="book.startTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item label="授权结束时间">
        <el-date-picker
          v-model="book.endTime"
          type="datetime"
          placeholder="选择结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item label="连载">
        <el-select v-model="book.serial" clearable placeholder="请选择">
          <el-option :value="1" label="连载中"/>
          <el-option :value="0" label="已完结"/>
        </el-select>
      </el-form-item>
      <el-form-item label="全本收费">
        <el-select v-model="book.charge" clearable placeholder="请选择">
          <el-option :value="1" label="收费"/>
          <el-option :value="0" label="免费"/>
        </el-select>
      </el-form-item>
      <el-form-item label="原创">
        <el-select v-model="book.original" clearable placeholder="请选择">
          <el-option :value="1" label="原创"/>
          <el-option :value="0" label="非原创"/>
        </el-select>
      </el-form-item>
      <el-form-item label="一级分类">
        <el-select v-model="book.firstSort" placeholder="请选择" @change="getSecondCategoryList">
          <el-option
            v-for="item in firstCategoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="book.secondSort" placeholder="请选择">
          <el-option
            v-for="item in secondCategoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简介">
        <el-input v-model="book.introduction" :rows="10" type="textarea"/>
      </el-form-item>
      <!-- TODO 书封 -->
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="save()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import book from '@/api/cms/book.js'
import category from '@/api/cms/category';

export default {
  data() {
    return {
      book: {
        firstSort: '',
        secondSort: ''
      },
      firstCategoryList: [],
      secondCategoryList: [],
      saveBtnDisabled: false // 点击禁用这个按钮，false 禁用 true禁用
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    save() {
      // 禁用按钮
      this.saveBtnDisabled = true
      // 调用add方法
      book.add(this.book)
        .then(response => {
          // 消息提示
          this.$message({
            type: 'success',
            message: '保存成功'
          })
          // 返回list列表页
          this.$router.push('/cms/book/list')
        })
    },
    getCategoryList() {
      category.getCategoryList()
        .then(response => {
          this.firstCategoryList = response.data.firstCategoryList
        })
    },
    getSecondCategoryList(row) {
      this.book.secondSort = ''
      for (let i = 0; i < this.firstCategoryList.length; i++) {
        if (this.firstCategoryList[i].id === row) {
          this.secondCategoryList = this.firstCategoryList[i].secondCategoryList
        }
      }
    }
  }
}
</script>

